package device

import "sipserver/utils"
import "gorm.io/gorm"
import "time"

type Device struct {
	ID       string
	IP       string
	Port     int
	Status   string
	Channels []Channel
}

type DevicePlatfrom struct {
	ID       string
	DeviceName       string
	Manufacturer   string
	Model     string
	Firmware string
}

type Channel struct {
	ID   string
	Name string
}

var devices = make(map[string]*Device)

func RegisterDevice(id, ip string, port int) {
	devices[id] = &Device{
		ID:     id,
		IP:     ip,
		Port:   port,
		Status: "Online",
	}
}

func GetDevice(id string) (*Device, bool) {
	device, exists := devices[id]
	return device, exists
}

func ListDevices() map[string]*Device {
	return devices
}

type DeviceBasic struct{
	gorm.Model
	DeviceID string
	DeviceName string
	Manufacturer string 
	ModelType string 
	Firmware string
	ClientIP string
	ClientPort int
	Salt string
	LoginTime time.Time
	HeartbeatTime time.Time
	LoginOutTime time.Time `gorm:"column:log_out_time" json:"log_out_time"`
	isLogout bool
	DeviceInfo string
 }


func (table *DeviceBasic) TableName() string {

	return "device_basic"
}

func GetDeviceList() []*DeviceBasic { 
	data := make([]*DeviceBasic,10)
	utils.DB.Find(&data)
	// for _,v : = range data {
	// 	fmt.Println(v)
	// }
	return data
}

func FindDeviceByName(name string) DeviceBasic {
	user := DeviceBasic{}
	utils.DB.Where("device_id = ?",name).Find(&user)
	return user
}

func CreateDevice(user DeviceBasic) *gorm.DB {
	
	return utils.DB.Create(&user)
}

func DeleteDevice(user DeviceBasic) *gorm.DB {
	
	return utils.DB.Delete(&user)
}

func UpdateDevice(user DeviceBasic) *gorm.DB {
	
	return utils.DB.Model(&user).Updates(DeviceBasic{DeviceName: user.DeviceName,Manufacturer: user.Manufacturer, ModelType: user.ModelType, Firmware: user.Firmware, ClientIP : user.ClientIP, ClientPort: user.ClientPort })
}